package com.orm.mybatisplus.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@MapperScan(basePackages = com.orm.mybatisplus.config.Master2DataSourceConfig.PACKAGE,sqlSessionTemplateRef = "master2SqlSessionTemplate")
public class Master2DataSourceConfig {
     static final String PACKAGE = "com.orm.mybatisplus.mapper.master2";
     static final String MAPPER_LOCATION = "classpath:mapper/master2/*.xml";

     @Value("${master2.url}")
     private String url;

     @Value("${master2.username}")
     private String user;

     @Value("${master2.password}")
     private String password;

     @Value("${master2.driverClassName}")
     private String driverClass;

//     @Bean(name = "master2DataSource")   //非分布式事务数据源
//     public DataSource master2DataSource(){
//         HikariDataSource hikariDataSource = new HikariDataSource();
//         hikariDataSource.setJdbcUrl(url);
//         hikariDataSource.setUsername(user);
//         hikariDataSource.setDriverClassName(driverClass);
//         hikariDataSource.setPassword(password);
//         hikariDataSource.setMinimumIdle(5);
//         hikariDataSource.setMaximumPoolSize(20);
//         hikariDataSource.setAutoCommit(true);
//         hikariDataSource.setPoolName("SpringBootDemoHikariCP");
//         hikariDataSource.setMaxLifetime(60000);
//         hikariDataSource.setConnectionTimeout(30000);
//         return  hikariDataSource;
//     }

    @Bean(name = "master2DataSource")   //分布式事务数据源
    public DataSource master2DataSource() throws SQLException {
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(url);
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(password);
        mysqlXaDataSource.setUser(user);
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        // 将本地事务注册到创 Atomikos全局事务
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("master2DataSource");
        xaDataSource.setMinPoolSize(5);
        xaDataSource.setMaxPoolSize(20);
        xaDataSource.setMaxLifetime(60000);
        xaDataSource.setBorrowConnectionTimeout(30);
        xaDataSource.setLoginTimeout(30);
        xaDataSource.setMaintenanceInterval(60);
        xaDataSource.setMaxIdleTime(60);
        return xaDataSource;
    }

//     非分布式事务
//     @Bean(name = "master2TransactionManager")
//     public DataSourceTransactionManager masterTransactionManager(@Qualifier("master2DataSource") DataSource dataSource){
//        return new DataSourceTransactionManager(dataSource);
//     }

     @Bean(name = "master2SqlSessionFactory")
     public SqlSessionFactory masterSqlSessionFactory(@Qualifier("master2DataSource") DataSource dataSource) throws Exception {
         final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
         sqlSessionFactoryBean.setDataSource(dataSource);
         sqlSessionFactoryBean.setTypeAliasesPackage("com.orm.mybatisplus.entity");
         MybatisConfiguration configuration = new MybatisConfiguration();
         configuration.setMapUnderscoreToCamelCase(true);
         sqlSessionFactoryBean.setConfiguration(configuration);
         sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(com.orm.mybatisplus.config.Master2DataSourceConfig.MAPPER_LOCATION));
         return sqlSessionFactoryBean.getObject();
     }

     @Bean(name = "master2SqlSessionTemplate")
     public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("master2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
         return new SqlSessionTemplate(sqlSessionFactory);
     }

}
